package com.sduept.nwld.mobile.manager;

import com.sduept.component.message.model.PersonnelConfigure;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.mobile.model.MobileUserInfo;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 移动端消息推送设置
 * @author ningfw
 * @date 2017-12-14
 *
 */
@Service
public class MobileUserInfoManager {

	@Autowired
	private SuperDAO superDao;
	@Autowired
	private PersonnelManager personnelManager;

	/**
	 * 查询所有接收消息的用户
	 * @return
	 */
	public List<MobileUserInfo> findReceivedUser() {

		String whereSql = " receiveFlag = 1 ";
		List<MobileUserInfo> userList = superDao.findByCondition(whereSql, null, MobileUserInfo.class);

		return userList;
	}

	/**
	 * 通过code获取消息接收人
	 * @param userCode
	 * @return
	 */
	public List<MobileUserInfo> findRecUserByCode(String userCode) {

		String whereSql = " code = ?1 ";
		Object[] whereArgs = new Object[1];
		whereArgs[0] = userCode;
		List<MobileUserInfo> userList = superDao.findByCondition(MobileUserInfo.class, whereSql, whereArgs);

		return userList;
	}

	/**
	 * 查询全部用户信息
	 * @param range
	 * @return
	 */
	public List<MobileUserInfo> findAll(int[] range) {

		return superDao.findByCondition(null, null, " order by createTime desc ", range, MobileUserInfo.class);
	}

	/**
	 * 查询全部用户信息
	 * @param range
	 * @return
	 */
	public List<PersonnelConfigure> findAllPerson(int[] range) {
		return superDao.findByCondition(null, null, null, range, PersonnelConfigure.class);
	}

	/**
	 * 统计全部用户数量
	 * @return
	 */
	public int countMobileUserInfo() {

		return (int) superDao.count(MobileUserInfo.class);
	}

	/**
	 * 根据条件查询
	 * @param whereHql
	 * @param range
	 * @return
	 */
	public List<PersonnelConfigure> findPersonByCondition(String whereHql, Object[] whereArgs, int[] range) {

		return superDao.findByCondition(whereHql, whereArgs, null, range, PersonnelConfigure.class);
	}

	/**
	 * 根据条件查询
	 * @param whereHql
	 * @param range
	 * @return
	 */
	public List<MobileUserInfo> findByCondition(String whereHql, Object[] whereArgs, int[] range) {

		return superDao.findByCondition(whereHql, whereArgs, " order by createTime desc ", range, MobileUserInfo.class);
	}

	/**
	 * 统计条件查询个数
	 * @param whereHql
	 * @return
	 */
	public int countByCondition(String whereHql, Object[] whereArgs) {

		return (int) superDao.count(whereHql, whereArgs, MobileUserInfo.class);
	}

	/**
	 * 新建
	 * @param created
	 * @return
	 */
	public MobileUserInfo createUser(MobileUserInfo created) {
		if (created != null) {
			try {
				created.setCreateTime(new Date());
				created = superDao.create(created, MobileUserInfo.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return created;
	}

	/**
	 * 新建
	 * @param created
	 * @return
	 */
	public PersonnelConfigure createPerson(PersonnelConfigure created) {
		if (created != null) {
			try {
				created = superDao.create(created, PersonnelConfigure.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return created;
	}

	/**
	 * 编辑
	 * @param selectUser
	 * @return
	 */
	public MobileUserInfo updateUser(MobileUserInfo selectUser) {
		if (selectUser != null) {
			try {
				return superDao.update(selectUser, MobileUserInfo.class); // update新的数据到这条Overhaulmag
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 编辑
	 * @param selectUser
	 * @return
	 */
	public PersonnelConfigure updatePerson(PersonnelConfigure selectUser) {
		if (selectUser != null) {
			try {
				return superDao.update(selectUser, PersonnelConfigure.class); // update新的数据到这条Overhaulmag
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 删除
	 * @param selectUser
	 */
	public void deleteUser(MobileUserInfo selectUser) {

		if (selectUser != null) {
			try {
				superDao.delete(selectUser.getId(), MobileUserInfo.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 删除
	 * @param selectUser
	 */
	public void deletePerson(PersonnelConfigure selectUser) {
		if (selectUser != null) {
			try {
				superDao.delete(selectUser.getId(), PersonnelConfigure.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 查询个人账户信息
	 * @param currentUser
	 * @return
	 */
	public List<MobileUserInfo> findSelfInfo(PersonnelEntityBean currentUser) {

		List<MobileUserInfo> muList = new ArrayList<MobileUserInfo>();
		if (currentUser != null) {

			String currentCode = currentUser.getCode();
			String whereHql = " code = ?1 ";
			Object[] whereArgs = new Object[1];
			whereArgs[0] = currentCode;
			muList = superDao.findByCondition(MobileUserInfo.class, whereHql, whereArgs);
		}
		return muList;
	}

	/**
	 * 根据4A账号查询手机用户
	 * @param account
	 * @return
	 */
	public MobileUserInfo findUserByAccount(String account) {

		MobileUserInfo mobileUserInfo = null;
		List<MobileUserInfo> mobileList = superDao.findByCondition(MobileUserInfo.class, "account=?1", account);
		if (mobileList.size() > 0) {
			mobileUserInfo = mobileList.get(0);
		}

		return mobileUserInfo;
	}
}
